# coding=utf8
__author__ = 'lijian'
import logging
import datetime

import lamp
import config
from utils import bcdUtil
from utils import byteUtil
import rtu
import time
import os


def fn_1(dataUnit):
    logging.info("do rtu ctrl")
    point = dataUnit.PN
    type = dataUnit.rawData[0]
    level = dataUnit.rawData[1]

    if type == 0:
        level = 3
    elif type == 1:
        level = 0

    if point == 0:
        rtu.action.ctrlToLevel(level)
    else:
        rtu.action.ctrlToLevel(level)

    return True


# 单灯遥控操作
def fn_2(dataUnit):
    logging.info("do lamp term ctrl")
    pn = dataUnit.PN

    state = dataUnit.rawData[0]
    level = dataUnit.rawData[1]

    if pn == 0:
        return lamp.action.broadcastCtrl(state, level)
    else:
        return lamp.action.pointCtrl(pn, state, level)
        # return True


# 单灯组遥控操作
def fn_3(dataUnit):
    logging.info("do term group ctrl")
    pn = dataUnit.PN
    state = dataUnit.rawData[0]
    level = dataUnit.rawData[1]

    lamp.action.broadcastCtrl(state,level)
    return True

    # if pn != 0:
    #     return lamp.action.groupCtrl(pn, state, level)
    # else:
    #     groups = lamp.manager.getGroup()
    #     for g in groups:
    #         lamp.action.groupCtrl(g, state, level)
    #         time.sleep(1)
    #     return True


# 单灯控制器轮询状态切换
def fn_9(dataUnit):
    logging.info("do set poll")
    enable = bool(dataUnit.rawData[0])
    discover = bool(dataUnit.rawData[1])

    config.updatePoll(enable, discover)

    return True


# 单灯控制器复位
def fn_10(dataUnit):
    logging.info("do reset term")
    deviceId = dataUnit.PN
    level = dataUnit.rawData[0]

    if deviceId != 0:
        term = lamp.manager.findTermByDeviceId(deviceId)
        if not term:
            return False
        else:
            term.reset(level)
            lamp.manager.saveLamps()
            return True
    else:
        terms=lamp.manager.getAllTerms()
        for term in terms:
            term.reset(level)
        lamp.manager.saveLamps()
        return True


# 单灯控制器组复位
def fn_11(dataUnit):
    logging.info("do reset group term")
    groupNo = dataUnit.PN
    level = dataUnit.rawData[0]

    if groupNo!=0:
        terms = lamp.manager.findTermByGroup(groupNo)
    else:
        terms=[]
        groups=lamp.manager.allGroup()
        for g in groups:
            ts=lamp.manager.findTermByGroup(g)
            terms.extend(ts)

    for term in terms:
        term.reset(level)
    if len(terms) > 0:
        lamp.manager.saveLamps()
    return True


# 新增单灯预约控制
def fn_12(dataUnit):
    logging.info("do add lamp holder plan task")

    lampNo = dataUnit.PN

    raw = dataUnit.rawData
    task = lamp.PlanTask(lamp.PlanTask.HOLDER_TASK)
    task.No = raw[0]
    task.planTime = bcdUtil.decodeDateTime(raw[1:7])
    task.optType = raw[7]
    task.level = raw[8]

    task.holderNo = lampNo

    lamp.manager.addPlanLampTasks(task)
    lamp.manager.savePlanTask()

    lamp.task.initTodayTempSchedule()

    return True


# 新增单灯组预约控制
def fn_13(dataUnit):
    logging.info("do add group plan task")
    groupNo = dataUnit.PN

    raw = dataUnit.rawData
    task = lamp.PlanTask(lamp.PlanTask.GROUP_TASK)
    task.No = raw[0]
    task.planTime = bcdUtil.decodeDateTime(raw[1:7])
    task.optType = raw[7]
    task.level = raw[8]

    task.groupNo = groupNo

    lamp.manager.addPlanGroupTask(task)
    lamp.manager.savePlanTask()

    lamp.task.initTodayTempSchedule()

    return True


# 删除单灯预约控制
def fn_14(dataUnit):
    logging.info("do delete lamp plan task")
    lampNo = dataUnit.PN
    No = dataUnit.rawData[0]

    if No == 0xFF:
        if lampNo == 0:
            lamp.manager.clearPlanLampTask()
        else:
            lamp.manager.deletePlanLampTask(lampNo, No)
    else:
        lamp.manager.deletePlanLampTask(lampNo, No)

    lamp.manager.savePlanTask()

    lamp.task.initTodayTempSchedule()
    return True


# 删除单灯组预约控制
def fn_15(dataUnit):
    logging.info("do delete group plan task")
    groupNo = dataUnit.PN
    No = dataUnit.rawData[0]

    if No == 0xFF:
        if groupNo == 0xFF:
            lamp.manager.clearPlanGroupTask()
        else:
            lamp.manager.deletePlanGroupTask(groupNo, No)
    else:
        lamp.manager.deletePlanGroupTask(groupNo, No)

    lamp.manager.savePlanTask()
    lamp.task.initTodayTempSchedule()
    return True

#新增控制器预约控制
def fn_17(dataUnit):
    logging.info("do add rtu term plan task")

    deviceId = dataUnit.PN

    raw = dataUnit.rawData
    task = lamp.PlanTask(lamp.PlanTask.TERM_TASK)
    task.No = raw[0]
    task.planTime = bcdUtil.decodeDateTime(raw[1:7])
    task.optType = raw[7]
    task.level = raw[8]

    task.deviceId = deviceId

    lamp.manager.addPlanTermTask(task)
    lamp.manager.savePlanTask()

    return True

#删除控制器预约控制
def fn_18(dataUnit):
    logging.info("do delete rtu plan task")
    deviceId = dataUnit.PN
    No = dataUnit.rawData[0]

    if No == 0xFF:
        if deviceId == 0:
            lamp.manager.clearPlanLampTask()
        else:
            lamp.manager.deleteTermPlanTask(deviceId,No)
    else:
        lamp.manager.deleteTermPlanTask(deviceId,No)

    lamp.manager.savePlanTask()

    return True

# 校时
def fn_31(dataUnit):
    logging.info("do set sync time")
    now = bcdUtil.decodeDateTime(dataUnit.rawData)
    logging.info("now time is: %s" % now.strftime("%Y-%m-%d %H:%M:%S"))

    #date [OPTION]... [MMDDhhmm[[CC]YY][.ss]] [+FORMAT]
    cmd='date "%02d%02d%02d%02d%d"' % (now.month,now.day,now.hour,now.minute,now.year)
    os.system(cmd)
    os.system("/work/externClock -w")
    logging.info("set time is:%r" % now)

    return True